<?php
$this->breadcrumbs=array(
	$model->name=>array('status','id'=>$model->id),
	Yii::t('project','Project Subdivision'),
);
?>

<?php echo $this->renderPartial('_wizard',array('model'=>$model, 'current'=>'sub')); ?>

<?php echo CHtml::link(Yii::t('project','Create Unit Project'), array('unitProject/create', 'pid'=>$model->id), array('class'=>'btn btn-primary')); ?>
<?php 
$dataProvider=new CActiveDataProvider('UnitProject', array(
		'criteria'=>array(
				'condition'=>'project_id=' . $model->id,
				'order'=>'pre_start_at DESC',
		),
		'pagination'=>array(
				'pageSize'=>10,
		),
));

$this->widget('bootstrap.widgets.TbGridView',array(
	'id'=>'unit-project-grid',
	'dataProvider'=>$dataProvider,
	'columns'=>array(
		array(
			'name' => 'name',
			'header'=>Yii::t('project','Unit Project Name'),
		),
// 		array(
// 			'name' => 'description',
// 			'header'=>Yii::t('project','Unit Project Description'),
// 		),
		array(
			'name' => 'appearance_score_total',
			'header'=>Yii::t('project','Unit Poject Appearance Score Rate'),
			'type' => 'raw',
			'value'=>function($data){
				$appearance_score_total = '0%';
				if($data->appearance_score_total != 0 && $data->appearance_score_got != 0) {
					$appearance_score_total = strval(round(($data->appearance_score_got/$data->appearance_score_total), 2) * 100) . '%';
					if(round(($data->appearance_score_got/$data->appearance_score_total), 2) * 100 <= 70) {
						$appearanceScoreContent = '<p class="text-error">' . $appearance_score_total . '</p>';
					} else if(round(($data->appearance_score_got/$data->appearance_score_total), 2) * 100 <= 85) {
						$appearanceScoreContent = '<p class="text-info">' . $appearance_score_total . '</p>';
					} else {
						$appearanceScoreContent = '<p class="text-success">' . $appearance_score_total . '</p>';
					}
				} else {
					$appearanceScoreContent = '<p class="muted">' . $appearance_score_total . '</p>';
				}
				return $appearanceScoreContent;
			},
		),
		array(
			'name' => 'description',
			'header'=>Yii::t('project','Unit Poject Status'),
			'type' => 'raw',
			'value'=>function($data){
				if($data->end_work_status == 1) {
					$qualifiedSegmentProjectNum = 0;
					$goodSegmentProjectNum = 0;
					$isUnitProjectStarted = false;
					foreach($data->segmentProjects as $eachSegmentProject) {
						if($data->end_work_status == 1) {
							$isUnitProjectStarted = true;
							if($eachSegmentProject->quality == 0) $qualifiedSegmentProjectNum++;
							else $goodSegmentProjectNum++;
						}
					}

					if( $isUnitProjectStarted) {
						if($data->appearance_score_total != 0) {
							if( $goodSegmentProjectNum/($goodSegmentProjectNum + $qualifiedSegmentProjectNum) > 0.7) {
								if($data->appearance_score_got/$data->appearance_score_total > 0.85)
									$qualityContent = '<p class="text-success">' . Yii::t('project','Good') . '</p>';
								else
									$qualityContent = '<p class="text-info">' . Yii::t('project','Qualified') . '</p>';
							} else {
								$qualityContent = '<p class="text-info">' . Yii::t('project','Qualified') . '</p>';
							}
						} else {
							$qualityContent = '<p class="muted">' . Yii::t('project','Unit Project Appearance Score not setup') . '</p>';
						}
					} else {
						$qualityContent = '<p class="muted">' . Yii::t('project','Not Started') . '</p>';
					}
				} else {
					$qualityContent = '<p class="muted">' . Yii::t('project','Unit Project Not Finished') . '</p>';
				}
				return $qualityContent;
			},
		),
		array(
			'name' => 'description',
			'header'=>Yii::t('project','Unit Poject Safety Status'),
			'type' => 'raw',
			'value'=>function($data){
				$isAnySafetyIssue = false;
				$safetyDetails = '';
				$isUnitProjectStarted = false;
				foreach($data->segmentProjects as $eachSegmentProject) {
					if($eachSegmentProject->start_work_status == 1) $isUnitProjectStarted = true;
					if($eachSegmentProject->safety == 1) {
						$isAnySafetyIssue = true;
						$safetyDetails .= $eachSegmentProject->safety_details . '/';
					}
				}
				if($isUnitProjectStarted) {
					if(!$isAnySafetyIssue) {
						$safetyContent = '<p class="text-success">' . Yii::t('project','No Sefety Issue') . '</p>';
					} else {
						$safetyContent = '<p class="text-error">' . Yii::t('project','Has Sefety Issue') . ' - ' . $safetyDetails . '</p>';
					}
				} else {
					$safetyContent = '<p class="muted">' . Yii::t('project','Not Started') . '</p>';
				}
				return $safetyContent;
			},
		),
		array(
			'name' => 'description',
			'header'=>Yii::t('project','Investment'),
			'type' => 'raw',
			'value'=>function($data){
						$investProgress = '0%';
						$progressBarClass = 'progress-success';
						$investProgressStr = '0%';
						
						$totalRealInvestUsed = 0;
						$totalPlanInvest = 0;
						foreach($data->segmentProjects as $eachSegmentProject) {
							$totalRealInvestUsed += $eachSegmentProject->real_invest_used;
							$totalPlanInvest += $eachSegmentProject->plan_invest;
						}
						
						if($totalRealInvestUsed != 0 && $totalPlanInvest != 0) { 
							$investProgress = strval(round(($totalRealInvestUsed/$totalPlanInvest), 2) * 100) . '%';
							$investProgressStr = $investProgress;
							
							if(($totalRealInvestUsed/$totalPlanInvest) > 1) {
								$investProgress = '100%';	//Over 100% is meaningless
								$progressBarClass = 'progress-danger';
							}
						} 

						return '<div class="progress ' . $progressBarClass . ' progress-striped" style="margin-bottom: 9px;">
									<div class="bar" style="width:' . $investProgress . '">' . $investProgressStr . '</div>
								</div>';
					},
		),
		array(
				'name' => 'description',
				'header'=>Yii::t('project','Schedule'),
				'type' => 'raw',
				'value'=>function($data){
								$projectStarted = 0;
								$projectFinished = 0;
								$isUnitProjectStarted = false;
								foreach($data->segmentProjects as $eachSegmentProject) {
									if($eachSegmentProject->start_work_status == 1) {
										$isUnitProjectStarted = true;
										$projectStarted++;
									}
									
									if($eachSegmentProject->end_work_status == 1) {
										$projectFinished++;
									}
								}
								if($isUnitProjectStarted) {
									$scheduleRate = round(($projectFinished/($projectStarted)), 2) * 100;
									$scheduleProgressStr = strval($scheduleRate) . '%';
									if($scheduleRate < 10) {
										$progressBarClass = 'progress-danger';
									} else if($scheduleRate < 40) {
										$progressBarClass = 'progress-warning';
									} else if($scheduleRate < 70) {
										$progressBarClass = 'progress-info';
									} else {
										$progressBarClass = 'progress-success';
									}
									$scheduleContent = '<div class="progress ' . $progressBarClass . ' progress-striped" style="margin-bottom: 9px;">
										<div class="bar" style="width:' . $scheduleProgressStr . '">' . $scheduleProgressStr . '</div>
									</div>';
								} else {
									$scheduleContent = '<p class="muted">' . Yii::t('project','Not Started') . '</p>';
								}
								return $scheduleContent;
							},
		),
		array(
				'class'=>'bootstrap.widgets.TbButtonColumn',
				'template'=>'{update}',
				'htmlOptions'=>array('width'=>'80px'),
				//'template'=>'{view}{update}{delete}',
				'buttons'=>array
				(
					'view' => array('url'=>'$this->grid->controller->createUrl("/unitProject/view", array("id"=>$data["id"]))'),
					'update' => array('url'=>'$this->grid->controller->createUrl("/unitProject/status", array("id"=>$data["id"]))'),
				),
		),
	),
));
?>

<div class="row-fluid">
	<div class="span12">
		<?php $form=$this->beginWidget('bootstrap.widgets.TbActiveForm',array(
							'id'=>'project-sub-form',
							'enableAjaxValidation'=>false,
		)); ?>
		
		<?php echo $form->hiddenField($model,'id'); ?>

		<div class="form-actions">
			<?php $this->widget('bootstrap.widgets.TbButton', array(
				'buttonType'=>'link',
				'type'=>'link',
				'url'=>array('project/status', 'id'=>$model->id),
				'label'=>Yii::t('common','Cancel'),
			)); ?>
			<?php $this->widget('bootstrap.widgets.TbButton', array(
				'buttonType'=>'submit',
				'type'=>'primary',
				'label'=>Yii::t('common','Save and Finish'),
			)); ?>
		</div>
	
		<?php $this->endWidget(); ?>
	</div>
</div>